package develop.game.exitfast;

import java.util.Random;

import develop.game.exitfast.R.layout;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AbsoluteLayout;
import android.widget.AbsoluteLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class PlayGameEasy extends Activity {
	function_execute fc = new function_execute();
	ImageView imgPlayer_1;
	ImageView imgPlayer_2;
	ImageView imgPlayer_3;
	ImageView imgDoor_1;
	ImageView imgDoor_2;
	ImageView imgDoor_3;
	AbsoluteLayout absLayout;

	AbsoluteLayout.LayoutParams absParams_1;
	AbsoluteLayout.LayoutParams absParams_2;
	AbsoluteLayout.LayoutParams absParams_3;

	AbsoluteLayout.LayoutParams doorParams_1;
	AbsoluteLayout.LayoutParams doorParams_2;
	AbsoluteLayout.LayoutParams doorParams_3;

	TextView lblTime;
	String strTime;
	TextView lblLevel;
	String strLevel;
	private int adjust = 50;
	int level = 0;
	int intSecond = 15 - level;

	DisplayMetrics metrics;
	int width;
	int height;
	// Display myDisplay;
	// int windowWidth;
	// int windowHeight;
	private boolean process_1 = false;
	private boolean process_2 = false;
	private boolean process_3 = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.easy_game);

		absLayout = (AbsoluteLayout) findViewById(R.id.AbsoluteLayout1);

		metrics = this.getResources().getDisplayMetrics();
		width = metrics.widthPixels;
		height = metrics.heightPixels;
		// myDisplay = this.getWindowManager().getDefaultDisplay();
		// windowWidth = myDisplay.getWidth();
		// windowHeight = myDisplay.getHeight();
		imgPlayer_1 = (ImageView) findViewById(R.id.imageExit_1);
		imgPlayer_2 = (ImageView) findViewById(R.id.ImageExit_2);
		imgPlayer_3 = (ImageView) findViewById(R.id.ImageExit_3);

		imgDoor_1 = (ImageView) findViewById(R.id.imgDoor_1);
		imgDoor_2 = (ImageView) findViewById(R.id.imgDoor_2);
		imgDoor_3 = (ImageView) findViewById(R.id.imgDoor_3);

		doorParams_1 = (AbsoluteLayout.LayoutParams) imgDoor_1
				.getLayoutParams();
		doorParams_1.x = fc.doorPositionX(width, adjust);
		doorParams_1.y = fc.doorPositionY(height, adjust);
		// doorParams_1.setMargins(fc.doorPositionX(width),
		// fc.doorPositionY(height), 100, 200);
		doorParams_2 = (AbsoluteLayout.LayoutParams) imgDoor_2
				.getLayoutParams();
		doorParams_2.x = fc.doorPositionX(width, adjust);
		doorParams_2.y = fc.doorPositionY(height, adjust);
		doorParams_3 = (AbsoluteLayout.LayoutParams) imgDoor_3
				.getLayoutParams();
		doorParams_3.x = fc.doorPositionX(width, adjust);
		doorParams_3.y = fc.doorPositionY(height, adjust);

		absParams_1 = (AbsoluteLayout.LayoutParams) imgPlayer_1
				.getLayoutParams();
		absParams_2 = (AbsoluteLayout.LayoutParams) imgPlayer_2
				.getLayoutParams();
		absParams_3 = (AbsoluteLayout.LayoutParams) imgPlayer_3
				.getLayoutParams();

		lblTime = (TextView) findViewById(R.id.lblTime);
		lblLevel = (TextView) findViewById(R.id.lblLevel);

		imgPlayer_1.setOnTouchListener(touchThis);
		imgPlayer_2.setOnTouchListener(touchThis);
		imgPlayer_3.setOnTouchListener(touchThis);

		Thread threadTimeWin = new Thread(new Runnable() {

			@Override
			public void run() {

				// TODO Auto-generated method stub
				while (true) {

					Message msg = myHandler.obtainMessage();
					myHandler.sendMessage(msg);
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}

		});
		threadTimeWin.start();
	}

	Handler myHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			lblTime.setText(strTime);
			lblLevel.setText(strLevel);
			intSecond--;
			strTime = "Time: " + intSecond;
			strLevel = "Level: " + (level + 1);
			if ((imgDoor_1.getVisibility() == View.GONE
					&& imgDoor_2.getVisibility() == View.GONE && imgDoor_3
					.getVisibility() == View.GONE)
					|| (imgPlayer_1.getVisibility() == View.GONE
							&& imgPlayer_2.getVisibility() == View.GONE && imgPlayer_3
							.getVisibility() == View.GONE)) {
				winGameCondition();
			}
			if (intSecond < 0) {
				loseGameCondition();
			}
		}
	};
	OnTouchListener touchThis = new OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			// TODO Auto-generated method stub
			// get pointer index from the event object
			// int pointerIndex = event.getActionIndex();

			// get pointer ID
			// int pointerId = event.getPointerId(pointerIndex);

			switch (event.getActionMasked()) {
			case MotionEvent.ACTION_MOVE:
				int x_cord = (int) event.getRawX();
				int y_cord = (int) event.getRawY();
				switch (v.getId()) {
				case R.id.imageExit_1:
					absParams_1.x = x_cord;
					absParams_1.y = y_cord;
					imgPlayer_1.setLayoutParams(absParams_1);

					if ((absParams_1.x == doorParams_1.x || absParams_1.y == doorParams_1.y)
							// && (doorParams_1.x == x_cord || doorParams_1.y ==
							// y_cord)
							&& process_1 == false
							&& imgDoor_1.getVisibility() != View.GONE) {
						imgDoor_1.setVisibility(View.GONE);
						imgPlayer_1.setVisibility(View.GONE);

						process_1 = true;
					} else if ((absParams_1.x == doorParams_2.x || absParams_1.y == doorParams_2.y)
							// && (doorParams_2.x == x_cord || doorParams_2.y ==
							// y_cord)
							&& process_1 == false
							&& imgDoor_2.getVisibility() != View.GONE) {
						imgDoor_2.setVisibility(View.GONE);
						imgPlayer_1.setVisibility(View.GONE);
						process_1 = true;

					} else if ((absParams_1.x == doorParams_3.x || absParams_1.y == doorParams_3.y)
							// && (doorParams_3.x == x_cord || doorParams_3.y ==
							// y_cord)
							&& process_1 == false
							&& imgDoor_3.getVisibility() != View.GONE) {
						imgDoor_3.setVisibility(View.GONE);
						imgPlayer_1.setVisibility(View.GONE);
						process_1 = true;

					}

					break;
				case R.id.ImageExit_2:
					absParams_2.x = x_cord;
					absParams_2.y = y_cord;
					imgPlayer_2.setLayoutParams(absParams_2);
					if ((absParams_2.x == doorParams_1.x || absParams_2.y == doorParams_1.y)
							&& process_2 == false
							&& imgDoor_1.getVisibility() != View.GONE) {
						imgDoor_1.setVisibility(View.GONE);
						imgPlayer_2.setVisibility(View.GONE);
						process_2 = true;

					} else if ((absParams_2.x == doorParams_2.x || absParams_2.y == doorParams_2.y)
							&& process_2 == false
							&& imgDoor_2.getVisibility() != View.GONE) {
						imgDoor_2.setVisibility(View.GONE);
						imgPlayer_2.setVisibility(View.GONE);
						process_2 = true;

					} else if ((absParams_2.x == doorParams_3.x || absParams_2.y == doorParams_3.y)
							&& process_2 == false
							&& imgDoor_3.getVisibility() != View.GONE) {
						imgDoor_3.setVisibility(View.GONE);
						imgPlayer_2.setVisibility(View.GONE);
						process_2 = true;

					}

					break;
				case R.id.ImageExit_3:
					absParams_3.x = x_cord;
					absParams_3.y = y_cord;
					imgPlayer_3.setLayoutParams(absParams_3);
					if ((absParams_3.x == doorParams_1.x || absParams_3.y == doorParams_1.y)
							&& process_3 == false
							&& imgDoor_1.getVisibility() != View.GONE) {
						imgDoor_1.setVisibility(View.GONE);
						imgPlayer_3.setVisibility(View.GONE);
						process_3 = true;

					} else if ((absParams_3.x == doorParams_2.x || absParams_3.y == doorParams_2.y)
							&& process_3 == false
							&& imgDoor_2.getVisibility() != View.GONE) {
						imgDoor_2.setVisibility(View.GONE);
						imgPlayer_3.setVisibility(View.GONE);
						process_3 = true;

					} else if ((absParams_3.x == doorParams_3.x || absParams_3.y == doorParams_3.y)
							&& process_3 == false
							&& imgDoor_3.getVisibility() != View.GONE) {
						imgDoor_3.setVisibility(View.GONE);
						imgPlayer_3.setVisibility(View.GONE);

						process_3 = true;
					}

					break;
				default:
					break;
				}

				break;

			default:
				break;
			}
			return true;
		}

	};

	private void display(String string) {
		// TODO Auto-generated method stub
		Toast.makeText(this, string, Toast.LENGTH_SHORT).show();
	}

	public void winGameCondition() {

		process_1 = false;
		process_2 = false;
		process_3 = false;
		imgDoor_1.setVisibility(View.VISIBLE);
		imgPlayer_1.setVisibility(View.VISIBLE);
		imgDoor_2.setVisibility(View.VISIBLE);
		imgPlayer_2.setVisibility(View.VISIBLE);
		imgDoor_3.setVisibility(View.VISIBLE);
		imgPlayer_3.setVisibility(View.VISIBLE);
		doorParams_1 = (AbsoluteLayout.LayoutParams) imgDoor_1
				.getLayoutParams();
		doorParams_1.x = fc.doorPositionX(width, adjust);
		doorParams_1.y = fc.doorPositionY(height, adjust);
		doorParams_2 = (AbsoluteLayout.LayoutParams) imgDoor_2
				.getLayoutParams();
		doorParams_2.x = fc.doorPositionX(width, adjust);
		if (Math.abs(doorParams_2.x - doorParams_1.x) < 50) {
			doorParams_2.x = fc.doorPositionX(width, adjust);
		}
		doorParams_2.y = fc.doorPositionY(height, adjust);
		if (Math.abs(doorParams_2.y - doorParams_1.y) < 50) {
			doorParams_2.y = fc.doorPositionX(height, adjust);
		}
		doorParams_3 = (AbsoluteLayout.LayoutParams) imgDoor_3
				.getLayoutParams();
		doorParams_3.x = fc.doorPositionX(width, adjust);
		doorParams_3.y = fc.doorPositionY(height, adjust);
		if (Math.abs(doorParams_3.x - doorParams_1.x) < 50) {
			doorParams_3.x = fc.doorPositionX(width, adjust);
		}
		if (Math.abs(doorParams_3.y - doorParams_1.y) < 50) {
			doorParams_3.y = fc.doorPositionX(height, adjust);
		}
		absParams_1.x = (int) convertDpToPixel(140, getApplication());
		absParams_1.y = (int) convertDpToPixel(440, getApplication());
		imgPlayer_1.setLayoutParams(absParams_1);
		absParams_2.x = (int) convertDpToPixel(250, getApplication());
		absParams_2.y = (int) convertDpToPixel(440, getApplication());
		imgPlayer_2.setLayoutParams(absParams_2);
		absParams_3.x = (int) convertDpToPixel(30, getApplication());
		absParams_3.y = (int) convertDpToPixel(440, getApplication());
		imgPlayer_3.setLayoutParams(absParams_3);
		level++;
		intSecond = 15 - level;
		if (intSecond <= 4) {
			intSecond = 4;
		}
		if (adjust > 10) {
			adjust -= 5;
		}
	}

	public void loseGameCondition() {

		finish();
	}

	public static float convertDpToPixel(float dp, Context context) {
		Resources resources = context.getResources();
		DisplayMetrics metrics = resources.getDisplayMetrics();
		float px = dp * (metrics.densityDpi / 160f);
		return px;
	}

	public static float convertPixelsToDp(float px, Context context) {
		Resources resources = context.getResources();
		DisplayMetrics metrics = resources.getDisplayMetrics();
		float dp = px / (metrics.densityDpi / 160f);
		return dp;
	}
}
